package com.woniuxy.order.controller;

import com.woniuxy.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单回调接口
 * 供 Flowable 服务在流程执行时回调
 */
@Slf4j
@RestController
@RequestMapping("/api/orders/callback")
@CrossOrigin(origins = "*")
public class OrderCallbackController {
    
    @Autowired
    private OrderService orderService;
    
    /**
     * 订单审批通过回调
     * 由 Flowable 流程中的服务任务调用
     */
    @PostMapping("/approve-success")
    public ResponseEntity<Map<String, Object>> onApproveSuccess(@RequestBody Map<String, Object> params) {
        log.info("收到订单审批通过回调，参数: {}", params);
        
        try {
            String orderId = (String) params.get("orderId");
            String orderNo = (String) params.get("orderNo");
            
            log.info("处理订单审批通过，订单ID: {}, 订单编号: {}", orderId, orderNo);
            
            // 更新订单状态为已通过
            orderService.updateOrderStatus(Long.valueOf(orderId), "APPROVED", null);
            
            // 发送成功通知
            sendSuccessNotification(orderNo);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "success");
            result.put("message", "订单审批通过处理成功");
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("处理订单审批通过回调失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "error");
            result.put("message", "处理失败: " + e.getMessage());
            
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 订单审批拒绝回调
     * 由 Flowable 流程中的服务任务调用
     */
    @PostMapping("/approve-reject")
    public ResponseEntity<Map<String, Object>> onApproveReject(@RequestBody Map<String, Object> params) {
        log.info("收到订单审批拒绝回调，参数: {}", params);
        
        try {
            String orderId = (String) params.get("orderId");
            String orderNo = (String) params.get("orderNo");
            String rejectReason = (String) params.get("rejectReason");
            String rejectStep = (String) params.get("rejectStep");
            String currentStep = (String) params.get("currentStep");
            
            log.info("处理订单审批拒绝，订单ID: {}, 订单编号: {}, 拒绝原因: {}, 拒绝步骤: {}", 
                orderId, orderNo, rejectReason, rejectStep);
            
            // 更新订单状态为已拒绝，包含拒绝步骤信息
            orderService.updateOrderStatusWithRejectStep(Long.valueOf(orderId), "REJECTED", rejectReason, rejectStep);
            
            // 发送拒绝通知
            sendFailureNotification(orderNo, rejectReason, currentStep);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "success");
            result.put("message", "订单审批拒绝处理成功");
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("处理订单审批拒绝回调失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "error");
            result.put("message", "处理失败: " + e.getMessage());
            
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 发送成功通知
     */
    private void sendSuccessNotification(String orderNo) {
        log.info("发送订单申请成功通知，订单编号: {}", orderNo);
        // TODO: 实现邮件/短信通知逻辑
    }
    
    /**
     * 更新订单当前步骤
     * 由 Flowable 流程中的服务任务调用
     */
    @PostMapping("/update-step")
    public ResponseEntity<Map<String, Object>> onUpdateStep(@RequestBody Map<String, Object> params) {
        log.info("收到订单步骤更新回调，参数: {}", params);
        
        try {
            String orderId = (String) params.get("orderId");
            String currentStep = (String) params.get("currentStep");
            String processInstanceId = (String) params.get("processInstanceId");
            
            log.info("更新订单步骤，订单ID: {}, 当前步骤: {}, 流程实例ID: {}", orderId, currentStep, processInstanceId);
            
            // 更新订单的当前步骤
            orderService.updateOrderStep(Long.valueOf(orderId), currentStep, processInstanceId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "success");
            result.put("message", "订单步骤更新成功");
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("处理订单步骤更新回调失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "error");
            result.put("message", "处理失败: " + e.getMessage());
            
            return ResponseEntity.ok(result);
        }
    }
    
    /**
     * 发送失败通知
     */
    private void sendFailureNotification(String orderNo, String rejectReason, String currentStep) {
        log.info("发送订单申请失败通知，订单编号: {}, 拒绝原因: {}, 拒绝步骤: {}", 
            orderNo, rejectReason, currentStep);
        // TODO: 实现邮件/短信通知逻辑
    }
}




